import java.util.Scanner;

/**
 * @author vivi
 * @description
 * @date 2024/4/26
 */
// 买卖股票（可多次）
// 贪心
public class demo2 {
    public static void main (String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] arr = new int[n];
        for(int i = 0; i < n; i++)
        {
            arr[i] = in.nextInt();
        }

        int ret = 0; // 卖出股票的价格

        for (int i = 1; i < n; i++) {
            if(arr[i] > arr[i-1]){
                ret += arr[i] - arr[i-1];
            }
        }

        System.out.println(ret);

    }
}
